<template>
	<view class="alert-wrap">
		<view class="top-container">
			<view>
				<navbar color="#ffffff" :isBack="false">{{$tt('navBar.alert')}}</navbar>
			</view>
			<view class="segmented-control">
				<u-subsection :list="tabList" :current="tabIndex" bgColor=" rgba(255,255,255,0.2)" activeColor="#3378FE"
					inactiveColor="#FFFFFF" @change="handleTabsClick"></u-subsection>
			</view>
		</view>

		<view class="log-content">
			<view class="nav-bar">
				<view class="left-wrap">
					<view v-if="!isSearch" style="margin-right: 20rpx;">
						<u-icon name="search" size="27" @click="isSearch = true"></u-icon>
					</view>
					<view v-else style="width: 100%;">
						<!-- #ifndef APP-NVUE -->
						<u-input :customStyle="{ padding: '17rpx 36rpx', background: '#FFFFFF' }"
							v-model="queryParams.deviceName" :placeholder="$tt('group.inputContent')" shape="circle"
							@clear="handleClearSearch" clearable>
						<!-- #endif -->
							<!-- #ifdef APP-NVUE -->
							<u--input :customStyle="{ padding: '17rpx 36rpx', background: '#FFFFFF' }"
								v-model="queryParams.deviceName" :placeholder="$tt('group.inputContent')" shape="circle"
								@clear="handleClearSearch" clearable>
							<!-- #endif -->
								<template slot="prefix">
									<u-icon name="search" color="rgb(192, 196, 204)" size="26"
										@click="isSearch = false"></u-icon>
								</template>
								<template slot="suffix">
									<view style="display: flex; flex-direction: row; align-items: center;">
										<span
											style="width: 0px; height: 14px; border: 1px solid #000000; opacity: 0.1;"></span>
										<span
											style="font-size: 14px; font-weight: 400; color: #3378FE; margin-left: 24rpx;"
											@click="handleSearch">{{$tt('common.search')}}</span>
									</view>
								</template>
						<!-- #ifndef APP-NVUE -->
						</u-input>
						<!-- #endif -->
						<!-- #ifdef APP-NVUE -->
						</u--input>
						<!-- #endif -->
					</view>
				</view>
			</view>
			<scroll-view v-show="total !== 0" class="data-list" :scroll-y="true" @refresherrefresh="handleRefresher"
				:refresher-enabled="true" :refresher-triggered="isTriggeredTop" refresher-background="#F1F3F9"
				@scrolltolower="handleScrollToLower">
				<view class="item-wrap" v-for="(item, index) in datas" :key="index" @click="handleDispose(item)">
					<view class="title">
						<view class="name">{{item.deviceName}}</view>
						<view class="status"
							:class="{'success': item.alertLevel === 1, 'warning': item.alertLevel === 2, 'error': item.alertLevel === 3}">
							{{ getaLertLevelDisplay(item) }}
						</view>
					</view>
					<view class="describe">
						<view class="top-line">
							<view class="item">{{$tt('alert.alertName')}}：{{item.alertName}}</view>
							<view class="item-end">
								{{$tt('alert.processState')}}：<text>
									{{ getaStatusDisplay(item) }}</text>
							</view>
						</view>
						<view class="item">{{$tt('alert.data')}}：<rich-text
								:nodes="getaDataDetailDisplay(item)"></rich-text>
						</view>
						<view class="item">{{$tt('alert.alertTime')}}：{{item.createTime}}</view>
					</view>
					<view class="tools">
						<view v-if="item.status === 2" class="btn"><u-button type="info" size="mini"
								:text="$tt('alert.process')" color="#c4c6c9"></u-button>
						</view>
					</view>
				</view>
				<u-loadmore :status="loadmoreStatus" v-if="total > queryParams.pageSize" marginTop="20" />
			</scroll-view>
			<u-empty mode="list" :show="total === 0 && isTriggeredTop === false" marginTop="60"
				:text="$tt('scene.emptyData')"></u-empty>
		</view>
		<u-loading-page :loading="loading" bg-color="#eef3f7" loadingText="FastBee.cn"></u-loading-page>
	</view>
</template>

<script>
	import {
		getAlertList,
		getAlertTemplateId
	} from '@/apis/modules/alertLog.js';
	import navbar from '@/components/navBar/index.vue';

	export default {
		components: {
			navbar
		},
		data() {
			return {
				loading: false,
				isSearch: true, // 是否开启搜索框
				// tabs列表
				tabIndex: 0,
				indicatorPosition: '0%',
				isTriggeredTop: false,
				tabList: [{
					id: null,
					name: this.$tt('alert.all'),
				}, {
					id: 2,
					name: this.$tt('alert.pending'),
				}, {
					id: 1,
					name: this.$tt('alert.untreated'),
				}, {
					id: 3,
					name: this.$tt('alert.processed'),
				}],
				queryParams: {
					alertName: null,
					status: null,
					pageNum: 1,
					pageSize: 10,
				},
				datas: [], // log列表数据
				total: 0, // 总条数
				loadmoreStatus: 'loadmore',
				tmpIds: [], //模板ids
			};
		},
		watch: {
			tabIndex(newIndex) {
				const percentage = (newIndex / (this.tabList.length)) * 100;
				this.indicatorPosition = `${percentage}%`;
				this.$emit('change', newIndex);
			}
		},
		onShow() {
			// #ifdef MP-WEIXIN
			// 检查用户是否登录
			if (!this.isLogin()) {
				uni.showModal({
					title: this.$tt('common.tips'),
					content: this.$tt('common.loginTips'),
					success: (res) => {
						if (res.confirm) {
							// 用户点击确定，跳转到登录页面
							uni.navigateTo({
								url: '/pages/login/index'
							});
						} else if (res.cancel) {
							uni.switchTab({
								url: '/pages/tabBar/home/index'
							})
							console.log('用户点击取消');
						}
					}
				});
			}
			// #endif
		},
		created() {
			this.getToken();
			if (this.token != '' && this.token != null) {
				this.getDatas();
				this.getTmplIds();
			}
		},
		mounted() {
			this.updateIndicatorPosition();
		},
		methods: {
			updateIndicatorPosition() {
				const percentage = (this.tabIndex / this.tabList.length) * 100;
				this.indicatorPosition = `${percentage}%`;
			},
			getToken() {
				// 本地缓存获取token
				this.token = uni.getStorageSync('token');
				// vuex存储token
				uni.$u.vuex('vuex_token', this.token);
			},
			//判断是否登录
			isLogin() {
				if (this.token == '' || this.token == null) {
					return false;
				} else {
					return true;
				}
			},
			// 获取列表数据
			getDatas() {
				// this.loading = true;
				getAlertList(this.queryParams).then(res => {
					const {
						code,
						rows,
						total
					} = res;
					if (this.queryParams.pageNum === 1) {
						this.datas = rows;
					} else {
						this.datas = this.datas.concat(rows);
					}
					this.total = total;
				});
			},
			handleSearch() {
				this.datas = [];
				this.queryParams.pageNum = 1;
				this.getDatas();
			},
			handleClearSearch() {
				this.handleSearch();
			},
			// 单击查询
			handleTabsClick(index) {
				this.tabIndex = index;
				this.queryParams.status = this.tabList[index].id;
				this.datas = [];
				this.queryParams.pageNum = 1;
				this.getDatas();
			},
			// 下拉刷新
			handleRefresher() {
				this.isTriggeredTop = true;
				this.datas = [];
				this.total = 0;
				this.queryParams.pageNum = 1;
				setTimeout(() => {
					this.getDatas();
					this.isTriggeredTop = false;
				}, 500)
			},
			// 上拉加载
			handleScrollToLower() {
				this.loadmoreStatus = 'loading';
				this.queryParams.pageNum = this.queryParams.pageNum + 1;
				if ((this.queryParams.pageNum - 1) * this.queryParams.pageSize >= this.total) {
					this.loadmoreStatus = 'nomore';
				} else {
					this.getDatas();
					this.loadmoreStatus = 'loadmore';
				}
			},
			// 上拉加载
			onReachBottom() {
				this.loadmoreStatus = 'loading';
				this.queryParams.pageNum = this.queryParams.pageNum + 1;
				if ((this.queryParams.pageNum - 1) * this.queryParams.pageSize >= this.total) {
					this.loadmoreStatus = 'nomore';
				} else {
					this.getDatas();
					this.loadmoreStatus = 'loadmore';
				}
			},
			// 下拉刷新
			onPullDownRefresh() {
				this.datas = [];
				this.queryParams.pageNum = 1;
				this.getDatas();
			},
			// 获取告警级别显示
			getaLertLevelDisplay(item) {
				const {
					alertLevel
				} = item;
				if (alertLevel === 1) {
					return this.$tt('alert.notice');
				} else if (alertLevel === 2) {
					return this.$tt('alert.minor');
				} else if (alertLevel === 3) {
					return this.$tt('alert.warning');
				}
			},
			// 获取告警状态显示
			getaStatusDisplay(item) {
				const {
					status
				} = item;
				if (status === 1) {
					return this.$tt('alert.untreated');
				} else if (status === 2) {
					return this.$tt('alert.pending');
				} else if (status === 3) {
					return this.$tt('alert.processed');
				}
			},
			// 获取告警数据内容
			getaDataDetailDisplay(item) {
				const {
					detail
				} = item;
				if (detail) {
					const detailObj = JSON.parse(detail);
					return `<div>id：<span style="color:#F56C6C">${detailObj.id}</span>，name：<span style="color:#F56C6C">${detailObj.name}</span>，value：<span style="color:#F56C6C">${detailObj.value}</span></span></div>`
				}
			},
			// 处理
			handleDispose(item) {
				// 订阅通知
				// #ifdef MP-WEIXIN
				uni.requestSubscribeMessage({
					tmplIds: [this.tmpIds],
					success(res) {
						console.log(res);
					},
					fail(err) {
						console.error(err); //失败
					}
				});
				// #endif
				uni.navigateTo({
					url: `/pagesB/alert/detail?alertLogId=${item.alertLogId}&source=2`
				});
			},
			// 获取模板id
			getTmplIds() {
				getAlertTemplateId().then(res => {
					if (res.code == 200) {
						if (res.msg != '') {
							this.tmpIds = res.msg;
						} else {
							console.log('模板id为空！');
						}
					} else {
						console.log(res.msg);
					}
				});
			},
		}
	};
</script>

<style>
	page {
		background: $uni-bg-color-grey;
	}
</style>
<style lang="scss" scoped>
	.alert-wrap {
		height: 100vh;
		display: flex;
		flex-direction: column;
		overflow-y: hidden;

		.top-container {
			position: relative;
			background-size: cover;
			background-image: url('@/static/common/bg.png');
			// #ifdef MP-WEIXIN
			min-height: 480rpx;
			// #endif
			// #ifdef H5
			min-height: 480rpx;
			// #endif
			// #ifdef APP-PLUS
			min-height: 550rpx;
			// #endif
			z-index: 0;

			.segmented-control {
				width: 84%;
				padding: 32rpx 34rpx;

				::v-deep .u-subsection--button {
					height: 68rpx;
					padding: 8rpx;
				}
			}
		}

		.log-content {
			flex: 1 1 auto;
			background: $uni-bg-color-grey;
			z-index: 1;
			border-top-left-radius: 40rpx;
			border-top-right-radius: 40rpx;
			padding: 0 30rpx;
			display: flex;
			flex-direction: column;
			overflow-y: auto;
			margin-top: -256rpx;
			// #ifndef H5 || APP-PLUS || MP-ALIPAY
			margin-top: -178rpx;
			// #endif
			z-index: 1;
			overflow-y: auto;


			.nav-bar {
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				align-items: center;
				padding-top: 20rpx;
				background: #eef3f7;

				.left-wrap {
					flex: 1;
					display: flex;
					flex-direction: row;
					align-items: center;
				}
			}

			.data-list {
				margin-top: 20rpx;
				flex: 1 1 auto;
				overflow-y: auto;
				//#ifdef H5
				padding-bottom: 70rpx;

				//#endif
				.item-wrap {
					border-radius: 20rpx;
					padding: 0 34rpx;
					background-color: #ffffff;
					margin-bottom: 26rpx;

					.title {
						display: flex;
						flex-direction: row;
						justify-content: space-between;
						align-items: center;
						border-bottom: 1px solid #eaecef;
						position: relative;
						height: 100rpx;

						.name {
							flex: 1;
							font-size: 32rpx;
							margin-right: 20rpx;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
						}

						.status {
							position: absolute;
							right: -34rpx;
							top: 0rpx;
							font-size: 20rpx;
							width: 128rpx;
							height: 48rpx;
							display: flex;
							align-items: center;
							justify-content: center;
							border-radius: 0 20rpx 0 20rpx;
						}
					}

					.describe {
						font-size: 28rpx;

						.top-line {
							display: flex;
							justify-content: space-between;

							.item {
								line-height: 40rpx;
								font-size: 26rpx;
								margin-top: 24rpx;
								width: 64%;

							}

							.item-end {
								display: flex;
								justify-content: flex-end;
								overflow: hidden;
								text-overflow: ellipsis;
								white-space: nowrap;
								line-height: 40rpx;
								font-size: 26rpx;
								margin-top: 24rpx;
								width: 35%;
							}
						}

						.item {
							display: flex;
							line-height: 40rpx;
							margin-top: 24rpx;
							font-size: 26rpx;
							width: 100%;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
						}
					}

					.tools {
						display: flex;
						flex-direction: row;
						justify-content: flex-end;
						padding-bottom: 36rpx;
					}
				}
			}
		}

		.success {
			color: #fff;
			background-color: #3378FE;
		}

		.warning {
			color: #fff;
			background-color: #f9ae3d;
		}

		.error {
			color: #fff;
			background-color: #f56c6c;
		}
	}
</style>